home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / Shells / tcsh / Source / ed.refresh.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  28.6 KB  |  1,163 lines

  1. /* $Header: /u/christos/src/tcsh-6.03/RCS/ed.refresh.c,v 3.12 1992/10/27 16:18:15 christos Exp $ */
  2. /*
  3.  * ed.refresh.c: Lower level screen refreshing functions
  4.  */
  5. /*-
  6.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37. #include "sh.h"
  38.  
  39. RCSID("$Id: ed.refresh.c,v 3.12 1992/10/27 16:18:15 christos Exp $")
  40.  
  41. #include "ed.h"
  42. /* #define DEBUG_UPDATE */
  43. /* #define DEBUG_REFRESH */
  44. /* #define DEBUG_LITERAL */
  45.  
  46. /* refresh.c -- refresh the current set of lines on the screen */
  47.  
  48. Char   *litptr[256];
  49. static int vcursor_h, vcursor_v;
  50.  
  51. static    void    Draw             __P((int));
  52. static    void    Vdraw             __P((int));
  53. static    void    update_line         __P((Char *, Char *, int));
  54. static    void    str_insert        __P((Char *, int, int, Char *, int));
  55. static    void    str_delete        __P((Char *, int, int, int));
  56. static    void    str_cp            __P((Char *, Char *, int));
  57. static    void    PutPlusOne        __P((int));
  58. static    void    cpy_pad_spaces        __P((Char *, Char *, int));
  59. #if defined(DEBUG_UPDATE) || defined(DEBUG_REFRESH) || defined(DEBUG_LITERAL)
  60. static    void    dprintf            __P((char *, ...));
  61. #ifdef DEBUG_UPDATE
  62. static    void    dprintstr        __P((char *, Char *, Char *));
  63.  
  64. static void
  65. dprintstr(str, f, t)
  66. char *str;
  67. Char *f, *t;
  68. {
  69.     dprintf("%s:\"", str);
  70.     while (f < t)
  71.     dprintf("%c", *f++ & ASCII);
  72.     dprintf("\"\r\n");
  73. #endif /* DEBUG_UPDATE */
  74.  
  75. /* dprintf():
  76.  *    Print to $DEBUGTTY, so that we can test editing on one pty, and 
  77.  *      print debugging stuff on another. Don't interrupt the shell while
  78.  *    debugging cause you'll mangle up the file descriptors!
  79.  */
  80. static void
  81. #if __STDC__
  82. dprintf(char *fmt, ...)
  83. #else
  84. dprintf(va_list)
  85.     va_dcl
  86. #endif /* __STDC__ */
  87. {
  88.     static int fd = -1;
  89.     char *dtty;
  90.  
  91.     if ((dtty = getenv("DEBUGTTY"))) {
  92.     int o;
  93.     va_list va;
  94. #if __STDC__
  95.     va_start(va, fmt);
  96. #else
  97.     char *fmt;
  98.     va_start(va);
  99.     fmt = va_arg(va, char *);
  100. #endif /* __STDC__ */
  101.  
  102.     if (fd == -1)
  103.         fd = open(dtty, O_RDWR);
  104.     o = SHOUT;
  105.     flush();
  106.     SHOUT = fd;
  107.     xvprintf(fmt, va);
  108.     va_end(va);
  109.     flush();
  110.     SHOUT = o;
  111.     }
  112. }
  113. #endif  /* DEBUG_UPDATE || DEBUG_REFRESH || DEBUG_LITERAL */
  114.  
  115. static void
  116. Draw(c)                /* draw c, expand tabs, ctl chars */
  117.     register int c;
  118. {
  119.     register Char ch = c & CHAR;
  120.  
  121.     if (Isprint(ch)) {
  122.     Vdraw(c);
  123.     return;
  124.     }
  125.     /* from wolman%crltrx.DEC@decwrl.dec.com (Alec Wolman) */
  126.     if (ch == '\n') {        /* expand the newline     */
  127.     /*
  128.      * Don't force a newline if Vdraw does it (i.e. we're at end of line)
  129.      * - or we will get two newlines and possibly garbage in between
  130.      */
  131.     int oldv = vcursor_v;
  132.  
  133.     Vdraw('\0');        /* assure end of line     */
  134.     if (oldv == vcursor_v) {
  135.         vcursor_h = 0;    /* reset cursor pos     */
  136.         vcursor_v++;
  137.     }
  138.     return;
  139.     }
  140.     if (ch == '\t') {        /* expand the tab      */
  141.     for (;;) {
  142.         Vdraw(' ');
  143.         if ((vcursor_h & 07) == 0)
  144.         break;        /* go until tab stop     */
  145.     }
  146.     }
  147.     else if (Iscntrl(ch)) {
  148.     Vdraw('^');
  149.     if (ch == '\177') {
  150.         Vdraw('?');
  151.     }
  152.     else {
  153.         /* uncontrolify it; works only for iso8859-1 like sets */
  154.         Vdraw((c | 0100));
  155.     }
  156.     }
  157.     else {
  158.     Vdraw('\\');
  159.     Vdraw(((c >> 6) & 7) + '0');
  160.     Vdraw(((c >> 3) & 7) + '0');
  161.     Vdraw((c & 7) + '0');
  162.     }
  163. }
  164.  
  165. static void
  166. Vdraw(c)            /* draw char c onto V lines */
  167.     register int c;
  168. {
  169. #ifdef DEBUG_REFRESH
  170. # ifdef SHORT_STRINGS
  171.     dprintf("Vdrawing %6.6o '%c'\r\n", c, c & ASCII);
  172. # else
  173.     dprintf("Vdrawing %3.3o '%c'\r\n", c, c);
  174. # endif /* SHORT_STRNGS */
  175. #endif  /* DEBUG_REFRESH */
  176.  
  177.     Vdisplay[vcursor_v][vcursor_h] = c;
  178.     vcursor_h++;        /* advance to next place */
  179.     if (vcursor_h >= TermH) {
  180.     Vdisplay[vcursor_v][TermH] = '\0';    /* assure end of line */
  181.     vcursor_h = 0;        /* reset it. */
  182.     vcursor_v++;
  183. #ifdef DEBUG_REFRESH
  184.     if (vcursor_v >= TermV) {    /* should NEVER happen. */
  185.         dprintf("\r\nVdraw: vcursor_v overflow! Vcursor_v == %d > %d\r\n",
  186.             vcursor_v, TermV);
  187.         abort();
  188.     }
  189. #endif /* DEBUG_REFRESH */
  190.     }
  191. }
  192.  
  193.  
  194. /*
  195.  *  Refresh()
  196.  *    draws the new virtual screen image from the current input
  197.  *      line, then goes line-by-line changing the real image to the new
  198.  *    virtual image. The routine to re-draw a line can be replaced
  199.  *    easily in hopes of a smarter one being placed there.
  200.  */
  201. static int OldvcV = 0;
  202. void
  203. Refresh()
  204. {
  205.     register int cur_line;
  206.     register Char *cp;
  207.     int     cur_h, cur_v = 0, new_vcv;
  208.     Char    oldgetting;
  209.     unsigned int litnum = 0;
  210.  
  211. #ifdef DEBUG_REFRESH
  212.     dprintf("PromptBuf = :%s:\r\n", short2str(PromptBuf));
  213.     dprintf("InputBuf = :%s:\r\n", short2str(InputBuf));
  214. #endif /* DEBUG_REFRESH */
  215.     oldgetting = GettingInput;
  216.     GettingInput = 0;        /* avoid re-entrance via SIGWINCH */
  217.  
  218.     /* reset the Vdraw cursor */
  219.     vcursor_h = 0;
  220.     vcursor_v = 0;
  221.  
  222.     /* draw prompt, we know it's ASCIZ */
  223.     for (cp = PromptBuf; *cp; cp++) {
  224.     if (*cp & LITERAL) {
  225.         if (litnum < (sizeof(litptr) / sizeof(litptr[0]))) {
  226.         litptr[litnum] = cp;
  227. #ifdef DEBUG_LITERAL
  228.         dprintf("litnum = %d, litptr = %x:\r\n",
  229.             litnum, litptr[litnum]);
  230. #endif /* DEBUG_LITERAL */
  231.         }
  232.         while (*cp & LITERAL)
  233.         cp++;
  234.         if (*cp)
  235.         Vdraw((int) (litnum++ | LITERAL));
  236.         else {
  237.         /*
  238.          * XXX: This is a bug, we lose the last literal, if it is not
  239.          * followed by a normal character, but it is too hard to fix
  240.          */
  241.         break;
  242.         }
  243.     }
  244.     else
  245.         Draw(*cp);
  246.     }
  247.     cur_h = -1;            /* set flag in case I'm not set */
  248.  
  249.     /* draw the current input buffer */
  250.     for (cp = InputBuf; (cp < LastChar); cp++) {
  251.     if (cp == Cursor) {
  252.         cur_h = vcursor_h;    /* save for later */
  253.         cur_v = vcursor_v;
  254.     }
  255.     Draw(*cp);
  256.     }
  257.  
  258.     if (cur_h == -1) {        /* if I havn't been set yet, I'm at the end */
  259.     cur_h = vcursor_h;
  260.     cur_v = vcursor_v;
  261.     }
  262.     new_vcv = vcursor_v;    /* must be done BEFORE the NUL is written */
  263.     Vdraw('\0');        /* put NUL on end */
  264.  
  265. #ifdef DEBUG_REFRESH
  266.     dprintf("TermH=%d, vcur_h=%d, vcur_v=%d, Vdisplay[0]=\r\n:%80.80s:\r\n",
  267.         TermH, vcursor_h, vcursor_v, short2str(Vdisplay[0]));
  268. #endif /* DEBUG_REFRESH */
  269.  
  270. #ifdef DEBUG_UPDATE
  271.     dprintf("updating %d lines.\r\n", new_vcv);
  272. #endif  /* DEBUG_UPDATE */
  273.     for (cur_line = 0; cur_line <= new_vcv; cur_line++) {
  274.     /* NOTE THAT update_line MAY CHANGE Display[cur_line] */
  275.     update_line(Display[cur_line], Vdisplay[cur_line], cur_line);
  276.  
  277.     /*
  278.      * Copy the new line to be the current one, and pad out with spaces
  279.      * to the full width of the terminal so that if we try moving the
  280.      * cursor by writing the character that is at the end of the
  281.      * screen line, it won't be a NUL or some old leftover stuff.
  282.      */
  283.     cpy_pad_spaces(Display[cur_line], Vdisplay[cur_line], TermH);
  284. #ifdef notdef
  285.     (void) Strncpy(Display[cur_line], Vdisplay[cur_line], TermH);
  286.     Display[cur_line][TermH] = '\0';    /* just in case */
  287. #endif
  288.     }
  289. #ifdef DEBUG_REFRESH
  290.     dprintf("\r\nvcursor_v = %d, OldvcV = %d, cur_line = %d\r\n",
  291.         vcursor_v, OldvcV, cur_line);
  292. #endif /* DEBUG_REFRESH */
  293.     if (OldvcV > new_vcv) {
  294.     for (; cur_line <= OldvcV; cur_line++) {
  295.         update_line(Display[cur_line], STRNULL, cur_line);
  296.         *Display[cur_line] = '\0';
  297.     }
  298.     }
  299.     OldvcV = new_vcv;        /* set for next time */
  300. #ifdef DEBUG_REFRESH
  301.     dprintf("\r\nCursorH = %d, CursorV = %d, cur_h = %d, cur_v = %d\r\n",
  302.         CursorH, CursorV, cur_h, cur_v);
  303. #endif /* DEBUG_REFRESH */
  304.     MoveToLine(cur_v);        /* go to where the cursor is */
  305.     MoveToChar(cur_h);
  306.     SetAttributes(0);        /* Clear all attributes */
  307.     flush();            /* send the output... */
  308.     GettingInput = oldgetting;    /* reset to old value */
  309. }
  310.  
  311. #ifdef notdef
  312. GotoBottom()
  313. {                /* used to go to last used screen line */
  314.     MoveToLine(OldvcV);
  315. }
  316.  
  317. #endif 
  318.  
  319. void
  320. PastBottom()
  321. {                /* used to go to last used screen line */
  322.     MoveToLine(OldvcV);
  323.     (void) putraw('\r');
  324.     (void) putraw('\n');
  325.     ClearDisp();
  326.     flush();
  327. }
  328.  
  329.  
  330. /* insert num characters of s into d (in front of the character) at dat,
  331.    maximum length of d is dlen */
  332. static void
  333. str_insert(d, dat, dlen, s, num)
  334.     register Char *d;
  335.     register int dat, dlen;
  336.     register Char *s;
  337.     register int num;
  338. {
  339.     register Char *a, *b;
  340.  
  341.     if (num <= 0)
  342.     return;
  343.     if (num > dlen - dat)
  344.     num = dlen - dat;
  345.  
  346. #ifdef DEBUG_REFRESH
  347.     dprintf("str_insert() starting: %d at %d max %d, d == \"%s\"\n",
  348.         num, dat, dlen, short2str(d));
  349.     dprintf("s == \"%s\"n", short2str(s));
  350. #endif /* DEBUG_REFRESH */
  351.  
  352.     /* open up the space for num chars */
  353.     if (num > 0) {
  354.     b = d + dlen - 1;
  355.     a = b - num;
  356.     while (a >= &d[dat])
  357.         *b-- = *a--;
  358.     d[dlen] = '\0';        /* just in case */
  359.     }
  360. #ifdef DEBUG_REFRESH
  361.     dprintf("str_insert() after insert: %d at %d max %d, d == \"%s\"\n",
  362.         num, dat, dlen, short2str(d));
  363.     dprintf("s == \"%s\"n", short2str(s));
  364. #endif /* DEBUG_REFRESH */
  365.  
  366.     /* copy the characters */
  367.     for (a = d + dat; (a < d + dlen) && (num > 0); num--)
  368.     *a++ = *s++;
  369.  
  370. #ifdef DEBUG_REFRESH
  371.     dprintf("str_insert() after copy: %d at %d max %d, d == \"%s\"\n",
  372.         num, dat, dlen, d, short2str(s));
  373.     dprintf("s == \"%s\"n", short2str(s));
  374. #endif /* DEBUG_REFRESH */
  375. }
  376.  
  377. /* delete num characters d at dat, maximum length of d is dlen */
  378. static void
  379. str_delete(d, dat, dlen, num)
  380.     register Char *d;
  381.     register int dat, dlen, num;
  382. {
  383.     register Char *a, *b;
  384.  
  385.     if (num <= 0)
  386.     return;
  387.     if (dat + num >= dlen) {
  388.     d[dat] = '\0';
  389.     return;
  390.     }
  391.  
  392. #ifdef DEBUG_REFRESH
  393.     dprintf("str_delete() starting: %d at %d max %d, d == \"%s\"\n",
  394.         num, dat, dlen, short2str(d));
  395. #endif /* DEBUG_REFRESH */
  396.  
  397.     /* open up the space for num chars */
  398.     if (num > 0) {
  399.     b = d + dat;
  400.     a = b + num;
  401.     while (a < &d[dlen])
  402.         *b++ = *a++;
  403.     d[dlen] = '\0';        /* just in case */
  404.     }
  405. #ifdef DEBUG_REFRESH
  406.     dprintf("str_delete() after delete: %d at %d max %d, d == \"%s\"\n",
  407.         num, dat, dlen, short2str(d));
  408. #endif /* DEBUG_REFRESH */
  409. }
  410.  
  411. static void
  412. str_cp(a, b, n)
  413.     register Char *a, *b;
  414.     register int n;
  415. {
  416.     while (n-- && *b)
  417.     *a++ = *b++;
  418. }
  419.  
  420.  
  421.  
  422. /* ****************************************************************
  423.     update_line() is based on finding the middle difference of each line
  424.     on the screen; vis:
  425.  
  426.                  /old first difference
  427.     /beginning of line   |              /old last same       /old EOL
  428.     v             v              v                    v
  429. old:    eddie> Oh, my little gruntle-buggy is to me, as lurgid as
  430. new:    eddie> Oh, my little buggy says to me, as lurgid as
  431.     ^             ^        ^               ^
  432.     \beginning of line   |        \new last same       \new end of line
  433.                  \new first difference
  434.  
  435.     all are character pointers for the sake of speed.  Special cases for
  436.     no differences, as well as for end of line additions must be handled.
  437. **************************************************************** */
  438.  
  439. /* Minimum at which doing an insert it "worth it".  This should be about
  440.  * half the "cost" of going into insert mode, inserting a character, and
  441.  * going back out.  This should really be calculated from the termcap
  442.  * data...  For the moment, a good number for ANSI terminals.
  443.  */
  444. #define MIN_END_KEEP    4
  445.  
  446. static void            /* could be changed to make it smarter */
  447. update_line(old, new, cur_line)
  448.     register Char *old, *new;
  449.     int     cur_line;
  450. {
  451.     register Char *o, *n, *p, c;
  452.     Char   *ofd, *ols, *oe, *nfd, *nls, *ne;
  453.     Char   *osb, *ose, *nsb, *nse;
  454.     int     fx, sx;
  455.  
  456.     /*
  457.      * find first diff
  458.      */
  459.     for (o = old, n = new; *o && (*o == *n); o++, n++)
  460.     continue;
  461.     ofd = o;
  462.     nfd = n;
  463.  
  464.     /*
  465.      * Find the end of both old and new
  466.      */
  467.     while (*o)
  468.     o++;
  469.     /* 
  470.      * Remove any trailing blanks off of the end, being careful not to
  471.      * back up past the beginning.
  472.      */
  473.     while (ofd < o) {
  474.     if (o[-1] != ' ')
  475.         break;
  476.     o--;
  477.     }
  478.     oe = o;
  479.     *oe = (Char) 0;
  480.   
  481.     while (*n)
  482.     n++;
  483.  
  484.     /* remove blanks from end of new */
  485.     while (nfd < n) {
  486.     if (n[-1] != ' ')
  487.         break;
  488.     n--;
  489.     }
  490.     ne = n;
  491.     *ne = (Char) 0;
  492.   
  493.     /*
  494.      * if no diff, continue to next line of redraw
  495.      */
  496.     if (*ofd == '\0' && *nfd == '\0') {
  497. #ifdef DEBUG_UPDATE
  498.     dprintf("no difference.\r\n");
  499. #endif /* DEBUG_UPDATE */
  500.     return;
  501.     }
  502.  
  503.     /*
  504.      * find last same pointer
  505.      */
  506.     while ((o > ofd) && (n > nfd) && (*--o == *--n))
  507.     continue;
  508.     ols = ++o;
  509.     nls = ++n;
  510.  
  511.     /*
  512.      * find same begining and same end
  513.      */
  514.     osb = ols;
  515.     nsb = nls;
  516.     ose = ols;
  517.     nse = nls;
  518.  
  519.     /*
  520.      * case 1: insert: scan from nfd to nls looking for *ofd
  521.      */
  522.     if (*ofd) {
  523.     for (c = *ofd, n = nfd; n < nls; n++) {
  524.         if (c == *n) {
  525.         for (o = ofd, p = n; p < nls && o < ols && *o == *p; o++, p++)
  526.             continue;
  527.         /*
  528.          * if the new match is longer and it's worth keeping, then we
  529.          * take it
  530.          */
  531.         if (((nse - nsb) < (p - n)) && (2 * (p - n) > n - nfd)) {
  532.             nsb = n;
  533.             nse = p;
  534.             osb = ofd;
  535.             ose = o;
  536.         }
  537.         }
  538.     }
  539.     }
  540.  
  541.     /*
  542.      * case 2: delete: scan from ofd to ols looking for *nfd
  543.      */
  544.     if (*nfd) {
  545.     for (c = *nfd, o = ofd; o < ols; o++) {
  546.         if (c == *o) {
  547.         for (n = nfd, p = o; p < ols && n < nls && *p == *n; p++, n++)
  548.             continue;
  549.         /*
  550.          * if the new match is longer and it's worth keeping, then we
  551.          * take it
  552.          */
  553.         if (((ose - osb) < (p - o)) && (2 * (p - o) > o - ofd)) {
  554.             nsb = nfd;
  555.             nse = n;
  556.             osb = o;
  557.             ose = p;
  558.         }
  559.         }
  560.     }
  561.     }
  562. #ifdef notdef
  563.     /*
  564.      * If `last same' is before `same end' re-adjust
  565.      */
  566.     if (ols < ose)
  567.     ols = ose;
  568.     if (nls < nse)
  569.     nls = nse;
  570. #endif
  571.  
  572.     /*
  573.      * Pragmatics I: If old trailing whitespace or not enough characters to
  574.      * save to be worth it, then don't save the last same info.
  575.      */
  576.     if ((oe - ols) < MIN_END_KEEP) {
  577.     ols = oe;
  578.     nls = ne;
  579.     }
  580.  
  581.     /*
  582.      * Pragmatics II: if the terminal isn't smart enough, make the data dumber
  583.      * so the smart update doesn't try anything fancy
  584.      */
  585.  
  586.     /*
  587.      * fx is the number of characters we need to insert/delete: in the
  588.      * beginning to bring the two same begins together
  589.      */
  590.     fx = (nsb - nfd) - (osb - ofd);
  591.     /*
  592.      * sx is the number of characters we need to insert/delete: in the end to
  593.      * bring the two same last parts together
  594.      */
  595.     sx = (nls - nse) - (ols - ose);
  596.  
  597.     if (!T_CanIns) {
  598.     if (fx > 0) {
  599.         osb = ols;
  600.         ose = ols;
  601.         nsb = nls;
  602.         nse = nls;
  603.     }
  604.     if (sx > 0) {
  605.         ols = oe;
  606.         nls = ne;
  607.     }
  608.     if ((ols - ofd) < (nls - nfd)) {
  609.         ols = oe;
  610.         nls = ne;
  611.     }
  612.     }
  613.     if (!T_CanDel) {
  614.     if (fx < 0) {
  615.         osb = ols;
  616.         ose = ols;
  617.         nsb = nls;
  618.         nse = nls;
  619.     }
  620.     if (sx < 0) {
  621.         ols = oe;
  622.         nls = ne;
  623.     }
  624.     if ((ols - ofd) > (nls - nfd)) {
  625.         ols = oe;
  626.         nls = ne;
  627.     }
  628.     }
  629.  
  630.     /*
  631.      * Pragmatics III: make sure the middle shifted pointers are correct if
  632.      * they don't point to anything (we may have moved ols or nls).
  633.      */
  634.     /* if the change isn't worth it, don't bother */
  635.     /* was: if (osb == ose) */
  636.     if ((ose - osb) < MIN_END_KEEP) {
  637.     osb = ols;
  638.     ose = ols;
  639.     nsb = nls;
  640.     nse = nls;
  641.     }
  642.  
  643.     /*
  644.      * Now that we are done with pragmatics we recompute fx, sx
  645.      */
  646.     fx = (nsb - nfd) - (osb - ofd);
  647.     sx = (nls - nse) - (ols - ose);
  648.  
  649. #ifdef DEBUG_UPDATE
  650.     dprintf("\n");
  651.     dprintf("ofd %d, osb %d, ose %d, ols %d, oe %d\n",
  652.         ofd - old, osb - old, ose - old, ols - old, oe - old);
  653.     dprintf("nfd %d, nsb %d, nse %d, nls %d, ne %d\n",
  654.         nfd - new, nsb - new, nse - new, nls - new, ne - new);
  655.     dprintf("xxx-xxx:\"00000000001111111111222222222233333333334\"\r\n");
  656.     dprintf("xxx-xxx:\"01234567890123456789012345678901234567890\"\r\n");
  657.     dprintstr("old- oe", old, oe);
  658.     dprintstr("new- ne", new, ne);
  659.     dprintstr("old-ofd", old, ofd);
  660.     dprintstr("new-nfd", new, nfd);
  661.     dprintstr("ofd-osb", ofd, osb);
  662.     dprintstr("nfd-nsb", nfd, nsb);
  663.     dprintstr("osb-ose", osb, ose);
  664.     dprintstr("nsb-nse", nsb, nse);
  665.     dprintstr("ose-ols", ose, ols);
  666.     dprintstr("nse-nls", nse, nls);
  667.     dprintstr("ols- oe", ols, oe);
  668.     dprintstr("nls- ne", nls, ne);
  669. #endif /* DEBUG_UPDATE */
  670.  
  671.     /*
  672.      * CursorV to this line cur_line MUST be in this routine so that if we
  673.      * don't have to change the line, we don't move to it. CursorH to first
  674.      * diff char
  675.      */
  676.     MoveToLine(cur_line);
  677.  
  678.     /*
  679.      * at this point we have something like this:
  680.      * 
  681.      * /old                  /ofd    /osb               /ose    /ols     /oe
  682.      * v.....................v       v..................v       v........v
  683.      * eddie> Oh, my fredded gruntle-buggy is to me, as foo var lurgid as
  684.      * eddie> Oh, my fredded quiux buggy is to me, as gruntle-lurgid as
  685.      * ^.....................^     ^..................^       ^........^ 
  686.      * \new                  \nfd  \nsb               \nse     \nls    \ne
  687.      * 
  688.      * fx is the difference in length between the the chars between nfd and
  689.      * nsb, and the chars between ofd and osb, and is thus the number of
  690.      * characters to delete if < 0 (new is shorter than old, as above),
  691.      * or insert (new is longer than short).
  692.      *
  693.      * sx is the same for the second differences.
  694.      */
  695.  
  696.     /*
  697.      * if we have a net insert on the first difference, AND inserting the net
  698.      * amount ((nsb-nfd) - (osb-ofd)) won't push the last useful character
  699.      * (which is ne if nls != ne, otherwise is nse) off the edge of the screen
  700.      * (TermH - 1) else we do the deletes first so that we keep everything we
  701.      * need to.
  702.      */
  703.  
  704.     /*
  705.      * if the last same is the same like the end, there is no last same part,
  706.      * otherwise we want to keep the last same part set p to the last useful
  707.      * old character
  708.      */
  709.     p = (ols != oe) ? oe : ose;
  710.  
  711.     /*
  712.      * if (There is a diffence in the beginning) && (we need to insert
  713.      * characters) && (the number of characters to insert is less than the term
  714.      * width) We need to do an insert! else if (we need to delete characters)
  715.      * We need to delete characters! else No insert or delete
  716.      */
  717.     if ((nsb != nfd) && fx > 0 && ((p - old) + fx < TermH)) {
  718. #ifdef DEBUG_UPDATE
  719.     dprintf("first diff insert at %d...\r\n", nfd - new);
  720. #endif  /* DEBUG_UPDATE */
  721.     /*
  722.      * Move to the first char to insert, where the first diff is.
  723.      */
  724.     MoveToChar(nfd - new);
  725.     /*
  726.      * Check if we have stuff to keep at end
  727.      */
  728.     if (nsb != ne) {
  729. #ifdef DEBUG_UPDATE
  730.         dprintf("with stuff to keep at end\r\n");
  731. #endif  /* DEBUG_UPDATE */
  732.         /*
  733.          * insert fx chars of new starting at nfd
  734.          */
  735.         if (fx > 0) {
  736. #ifdef DEBUG_UPDATE
  737.         if (!T_CanIns)
  738.             dprintf("   ERROR: cannot insert in early first diff\n");
  739. #endif  /* DEBUG_UPDATE */
  740.         Insert_write(nfd, fx);
  741.         str_insert(old, ofd - old, TermH, nfd, fx);
  742.         }
  743.         /*
  744.          * write (nsb-nfd) - fx chars of new starting at (nfd + fx)
  745.          */
  746.         so_write(nfd + fx, (nsb - nfd) - fx);
  747.         str_cp(ofd + fx, nfd + fx, (nsb - nfd) - fx);
  748.     }
  749.     else {
  750. #ifdef DEBUG_UPDATE
  751.         dprintf("without anything to save\r\n");
  752. #endif  /* DEBUG_UPDATE */
  753.         so_write(nfd, (nsb - nfd));
  754.         str_cp(ofd, nfd, (nsb - nfd));
  755.         /*
  756.          * Done
  757.          */
  758.         return;
  759.     }
  760.     }
  761.     else if (fx < 0) {
  762. #ifdef DEBUG_UPDATE
  763.     dprintf("first diff delete at %d...\r\n", ofd - old);
  764. #endif  /* DEBUG_UPDATE */
  765.     /*
  766.      * move to the first char to delete where the first diff is
  767.      */
  768.     MoveToChar(ofd - old);
  769.     /*
  770.      * Check if we have stuff to save
  771.      */
  772.     if (osb != oe) {
  773. #ifdef DEBUG_UPDATE
  774.         dprintf("with stuff to save at end\r\n");
  775. #endif  /* DEBUG_UPDATE */
  776.         /*
  777.          * fx is less than zero *always* here but we check for code
  778.          * symmetry
  779.          */
  780.         if (fx < 0) {
  781. #ifdef DEBUG_UPDATE
  782.         if (!T_CanDel)
  783.             dprintf("   ERROR: cannot delete in first diff\n");
  784. #endif /* DEBUG_UPDATE */
  785.         DeleteChars(-fx);
  786.         str_delete(old, ofd - old, TermH, -fx);
  787.         }
  788.         /*
  789.          * write (nsb-nfd) chars of new starting at nfd
  790.          */
  791.         so_write(nfd, (nsb - nfd));
  792.         str_cp(ofd, nfd, (nsb - nfd));
  793.  
  794.     }
  795.     else {
  796. #ifdef DEBUG_UPDATE
  797.         dprintf("but with nothing left to save\r\n");
  798. #endif  /* DEBUG_UPDATE */
  799.         /*
  800.          * write (nsb-nfd) chars of new starting at nfd
  801.          */
  802.         so_write(nfd, (nsb - nfd));
  803. #ifdef DEBUG_REFRESH
  804.         dprintf("cleareol %d\n", (oe - old) - (ne - new));
  805. #endif  /* DEBUG_UPDATE */
  806.         ClearEOL((oe - old) - (ne - new));
  807.         /*
  808.          * Done
  809.          */
  810.         return;
  811.     }
  812.     }
  813.     else
  814.     fx = 0;
  815.  
  816.     if (sx < 0) {
  817. #ifdef DEBUG_UPDATE
  818.     dprintf("second diff delete at %d...\r\n", (ose - old) + fx);
  819. #endif  /* DEBUG_UPDATE */
  820.     /*
  821.      * Check if we have stuff to delete
  822.      */
  823.     /*
  824.      * fx is the number of characters inserted (+) or deleted (-)
  825.      */
  826.  
  827.     MoveToChar((ose - old) + fx);
  828.     /*
  829.      * Check if we have stuff to save
  830.      */
  831.     if (ols != oe) {
  832. #ifdef DEBUG_UPDATE
  833.         dprintf("with stuff to save at end\r\n");
  834. #endif  /* DEBUG_UPDATE */
  835.         /*
  836.          * Again a duplicate test.
  837.          */
  838.         if (sx < 0) {
  839. #ifdef DEBUG_UPDATE
  840.         if (!T_CanDel)
  841.             dprintf("   ERROR: cannot delete in second diff\n");
  842. #endif  /* DEBUG_UPDATE */
  843.         DeleteChars(-sx);
  844.         }
  845.  
  846.         /*
  847.          * write (nls-nse) chars of new starting at nse
  848.          */
  849.         so_write(nse, (nls - nse));
  850.     }
  851.     else {
  852. #ifdef DEBUG_UPDATE
  853.         dprintf("but with nothing left to save\r\n");
  854. #endif /* DEBUG_UPDATE */
  855.         so_write(nse, (nls - nse));
  856. #ifdef DEBUG_REFRESH
  857.         dprintf("cleareol %d\n", (oe - old) + fx - (ne - new));
  858. #endif /* DEBUG_UPDATE */
  859.         ClearEOL((oe - old) + fx - (ne - new));
  860.     }
  861.     }
  862.  
  863.     /*
  864.      * if we have a first insert AND WE HAVEN'T ALREADY DONE IT...
  865.      */
  866.     if ((nsb != nfd) && (osb - ofd) <= (nsb - nfd) && (fx == 0)) {
  867. #ifdef DEBUG_UPDATE
  868.     dprintf("late first diff insert at %d...\r\n", nfd - new);
  869. #endif /* DEBUG_UPDATE */
  870.  
  871.     MoveToChar(nfd - new);
  872.     /*
  873.      * Check if we have stuff to keep at the end
  874.      */
  875.     if (nsb != ne) {
  876. #ifdef DEBUG_UPDATE
  877.         dprintf("with stuff to keep at end\r\n");
  878. #endif /* DEBUG_UPDATE */
  879.         /* 
  880.          * We have to recalculate fx here because we set it
  881.          * to zero above as a flag saying that we hadn't done
  882.          * an early first insert.
  883.          */
  884.         fx = (nsb - nfd) - (osb - ofd);
  885.         if (fx > 0) {
  886.         /*
  887.          * insert fx chars of new starting at nfd
  888.          */
  889. #ifdef DEBUG_UPDATE
  890.         if (!T_CanIns)
  891.             dprintf("   ERROR: cannot insert in late first diff\n");
  892. #endif /* DEBUG_UPDATE */
  893.         Insert_write(nfd, fx);
  894.         str_insert(old, ofd - old, TermH, nfd, fx);
  895.         }
  896.  
  897.         /*
  898.          * write (nsb-nfd) - fx chars of new starting at (nfd + fx)
  899.          */
  900.         so_write(nfd + fx, (nsb - nfd) - fx);
  901.         str_cp(ofd + fx, nfd + fx, (nsb - nfd) - fx);
  902.     }
  903.     else {
  904. #ifdef DEBUG_UPDATE
  905.         dprintf("without anything to save\r\n");
  906. #endif /* DEBUG_UPDATE */
  907.         so_write(nfd, (nsb - nfd));
  908.         str_cp(ofd, nfd, (nsb - nfd));
  909.     }
  910.     }
  911.  
  912.     /*
  913.      * line is now NEW up to nse
  914.      */
  915.     if (sx >= 0) {
  916. #ifdef DEBUG_UPDATE
  917.     dprintf("second diff insert at %d...\r\n", nse - new);
  918. #endif /* DEBUG_UPDATE */
  919.     MoveToChar(nse - new);
  920.     if (ols != oe) {
  921. #ifdef DEBUG_UPDATE
  922.         dprintf("with stuff to keep at end\r\n");
  923. #endif /* DEBUG_UPDATE */
  924.         if (sx > 0) {
  925.         /* insert sx chars of new starting at nse */
  926. #ifdef DEBUG_UPDATE
  927.         if (!T_CanIns)
  928.             dprintf("   ERROR: cannot insert in second diff\n");
  929. #endif /* DEBUG_UPDATE */
  930.         Insert_write(nse, sx);
  931.         }
  932.  
  933.         /*
  934.          * write (nls-nse) - sx chars of new starting at (nse + sx)
  935.          */
  936.         so_write(nse + sx, (nls - nse) - sx);
  937.     }
  938.     else {
  939. #ifdef DEBUG_UPDATE
  940.         dprintf("without anything to save\r\n");
  941. #endif /* DEBUG_UPDATE */
  942.         so_write(nse, (nls - nse));
  943.  
  944.         /*
  945.              * No need to do a clear-to-end here because we were doing
  946.          * a second insert, so we will have over written all of the
  947.          * old string.
  948.          */
  949.     }
  950.     }
  951. #ifdef DEBUG_UPDATE
  952.     dprintf("done.\r\n");
  953. #endif /* DEBUG_UPDATE */
  954. }
  955.  
  956.  
  957. static void
  958. cpy_pad_spaces(dst, src, width)
  959.     register Char *dst, *src;
  960.     register int width;
  961. {
  962.     register int i;
  963.  
  964.     for (i = 0; i < width; i++) {
  965.     if (*src == (Char) 0)
  966.         break;
  967.     *dst++ = *src++;
  968.     }
  969.  
  970.     while (i < width) {
  971.     *dst++ = ' ';
  972.     i++;
  973.     }
  974.     *dst = (Char) 0;
  975. }
  976.  
  977. void
  978. RefCursor()
  979. {                /* only move to new cursor pos */
  980.     register Char *cp, c;
  981.     register int h, th, v;
  982.  
  983.     /* first we must find where the cursor is... */
  984.     h = 0;
  985.     v = 0;
  986.     th = TermH;            /* optimize for speed */
  987.  
  988.     for (cp = PromptBuf; *cp; cp++) {    /* do prompt */
  989.     if (*cp & LITERAL)
  990.         continue;
  991.     c = *cp & CHAR;        /* extra speed plus strip the inverse */
  992.     h++;            /* all chars at least this long */
  993.  
  994.     /* from wolman%crltrx.DEC@decwrl.dec.com (Alec Wolman) */
  995.     /* lets handle newline as part of the prompt */
  996.  
  997.     if (c == '\n') {
  998.         h = 0;
  999.         v++;
  1000.     }
  1001.     else {
  1002.         if (c == '\t') {    /* if a tab, to next tab stop */
  1003.         while (h & 07) {
  1004.             h++;
  1005.         }
  1006.         }
  1007.         else if (Iscntrl(c)) {    /* if control char */
  1008.         h++;
  1009.         if (h > th) {    /* if overflow, compensate */
  1010.             h = 1;
  1011.             v++;
  1012.         }
  1013.         }
  1014.         else if (!Isprint(c)) {
  1015.         h += 3;
  1016.         if (h > th) {    /* if overflow, compensate */
  1017.             h = h - th;
  1018.             v++;
  1019.         }
  1020.         }
  1021.     }
  1022.  
  1023.     if (h >= th) {        /* check, extra long tabs picked up here also */
  1024.         h = 0;
  1025.         v++;
  1026.     }
  1027.     }
  1028.  
  1029.     for (cp = InputBuf; cp < Cursor; cp++) {    /* do input buffer to Cursor */
  1030.     c = *cp & CHAR;        /* extra speed plus strip the inverse */
  1031.     h++;            /* all chars at least this long */
  1032.  
  1033.     if (c == '\n') {    /* handle newline in data part too */
  1034.         h = 0;
  1035.         v++;
  1036.     }
  1037.     else {
  1038.         if (c == '\t') {    /* if a tab, to next tab stop */
  1039.         while (h & 07) {
  1040.             h++;
  1041.         }
  1042.         }
  1043.         else if (Iscntrl(c)) {    /* if control char */
  1044.         h++;
  1045.         if (h > th) {    /* if overflow, compensate */
  1046.             h = 1;
  1047.             v++;
  1048.         }
  1049.         }
  1050.         else if (!Isprint(c)) {
  1051.         h += 3;
  1052.         if (h > th) {    /* if overflow, compensate */
  1053.             h = h - th;
  1054.             v++;
  1055.         }
  1056.         }
  1057.     }
  1058.  
  1059.     if (h >= th) {        /* check, extra long tabs picked up here also */
  1060.         h = 0;
  1061.         v++;
  1062.     }
  1063.     }
  1064.  
  1065.     /* now go there */
  1066.     MoveToLine(v);
  1067.     MoveToChar(h);
  1068.     flush();
  1069. }
  1070.  
  1071. static void
  1072. PutPlusOne(c)
  1073.     int    c;
  1074. {
  1075.     (void) putraw(c);
  1076.     Display[CursorV][CursorH++] = c;
  1077.     if (CursorH >= TermH) {    /* if we must overflow */
  1078.     CursorH = 0;
  1079.     CursorV++;
  1080.     OldvcV++;
  1081.     if (T_Margin & MARGIN_AUTO) {
  1082.         if (T_Margin & MARGIN_MAGIC) {
  1083.         (void) putraw(' ');
  1084.         (void) putraw('\b');
  1085.         }
  1086.     }
  1087.     else {
  1088.         (void) putraw('\r');
  1089.         (void) putraw('\n');
  1090.     }
  1091.     }
  1092. }
  1093.  
  1094. void
  1095. RefPlusOne()
  1096. {                /* we added just one char, handle it fast *//* a
  1097.                  * ssumes that screen cursor == real cursor */
  1098.     register Char c, mc;
  1099.  
  1100.     c = Cursor[-1] & CHAR;    /* the char we just added */
  1101.  
  1102.     if (c == '\t' || Cursor != LastChar) {
  1103.     Refresh();        /* too hard to handle */
  1104.     return;
  1105.     }                /* else (only do at end of line, no TAB) */
  1106.  
  1107.     if (Iscntrl(c)) {        /* if control char, do caret */
  1108.     mc = (c == '\177') ? '?' : (c | 0100);
  1109.     PutPlusOne('^');
  1110.     PutPlusOne(mc);
  1111.     }
  1112.     else if (Isprint(c)) {    /* normal char */
  1113.     PutPlusOne(c);
  1114.     }
  1115.     else {
  1116.     PutPlusOne('\\');
  1117.     PutPlusOne(((c >> 6) & 7) + '0');
  1118.     PutPlusOne(((c >> 3) & 7) + '0');
  1119.     PutPlusOne((c & 7) + '0');
  1120.     }
  1121.     flush();
  1122. }
  1123.  
  1124. /* clear the screen buffers so that new new prompt starts fresh. */
  1125.  
  1126. void
  1127. ClearDisp()
  1128. {
  1129.     register int i;
  1130.  
  1131.     CursorV = 0;        /* clear the display buffer */
  1132.     CursorH = 0;
  1133.     for (i = 0; i < TermV; i++)
  1134.     Display[i][0] = '\0';
  1135.     OldvcV = 0;
  1136. }
  1137.  
  1138. void
  1139. ClearLines()
  1140. {                /* Make sure all lines are *really* blank */
  1141.     register int i;
  1142.  
  1143.     if (T_CanCEOL) {
  1144.     /*
  1145.      * Clear the lines from the bottom up so that if we try moving
  1146.      * the cursor down by writing the character that is at the end
  1147.      * of the screen line, we won't rewrite a character that shouldn't
  1148.      * be there.
  1149.      */
  1150.     for (i = OldvcV; i >= 0; i--) {    /* for each line on the screen */
  1151.         MoveToLine(i);
  1152.         MoveToChar(0);
  1153.         ClearEOL(TermH);
  1154.     }
  1155.     }
  1156.     else {
  1157.     MoveToLine(OldvcV);    /* go to last line */
  1158.     (void) putraw('\r');    /* go to BOL */
  1159.     (void) putraw('\n');    /* go to new line */
  1160.     }
  1161. }
  1162.